#include <iostream>
#include <queue>
#include <string>
using namespace std;

const int kMaxH = 1020;
const int kMaxW = 1020;
const int kInf = 1e9;

string g[kMaxH];
int dis[kMaxH][kMaxW];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool Check(int x, int y, int h, int w, int d) {
  return x >= 0 && x < h && y >= 0 && y < w && g[x][y] == '.' && dis[x][y] > d;
}

int BFS(int h, int w, int c_h, int c_w, int d_h, int d_w) {
  queue<pair<int, int>> que[2];
  for (int i = 0; i < h; i++) {
    fill(dis[i], dis[i] + w, kInf);
  }

  dis[c_h][c_w] = 0;
  que[0].emplace(c_h, c_w);

  for (int magic = 0; que[0].size() + que[1].size(); magic++) {
    for (queue<pair<int, int>> &now = que[magic & 1], &next = que[(magic & 1) ^ 1]; !now.empty(); now.pop()) {
      int x = now.front().first, y = now.front().second;

      if (dis[x][y] != magic) {
        continue;
      }
      for (int i = 0; i < 4; i++) {
        int nx = x + dir[i][0], ny = y + dir[i][1];

        if (Check(nx, ny, h, w, magic)) {
          dis[nx][ny] = magic;
          now.emplace(nx, ny);
        }
      }

      for (int dx = -2; dx <= 2; dx++) {
        for (int dy = -2; dy <= 2; dy++) {
          int nx = x + dx, ny = y + dy;

          if (Check(nx, ny, h, w, magic + 1)) {
            dis[nx][ny] = magic + 1;
            next.emplace(nx, ny);
          }
        }
      }
    }
    if (dis[d_h][d_w] != kInf) {
      return dis[d_h][d_w];
    }
  }
  return -1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int h, w;
  int c_h, c_w;
  int d_h, d_w;

  cin >> h >> w;
  cin >> c_h >> c_w;
  cin >> d_h >> d_w;
  c_h--, c_w--;
  d_h--, d_w--;
  for (int i = 0; i < h; i++) {
    cin >> g[i];
  }
  cout << BFS(h, w, c_h, c_w, d_h, d_w) << "\n";
  return 0;
}